package com.zhugang.week05;

/**
 * @program algorithms
 * @description: perfectSquare
 * @author: chanzhugang
 * @create: 2022/07/06 15:03
 */
public class PerfectSquare {

    public static void main(String[] args) {
        PerfectSquare perfectSquare = new PerfectSquare();
        boolean flag = perfectSquare.isPerfectSquare(6);
        System.out.println(flag);
    }

    /**
     * 367 有效的完全平方数
     *
     * @param num
     * @return
     */
    public boolean isPerfectSquare(int num) {
        // 1 - num 之间选择一个数的平方等于num
        // 标准二分
        int low = 1;
        int high = num;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            long res = (long) mid * mid;
            if (res == num) {
                return true;
            } else if (res < num) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return false;
    }
}